import axios from 'axios'
import { BASE_URL, TIMEOUT } from './config'

class $http {
  constructor(baseURL, timeout) {
    this.instance = axios.create({
      baseURL,
      timeout
    })

    this.instance.interceptors.response.use((res) => {
      return res.data
    }, err => {
      return err
    })
  }

  request(config) {
    return this.instance.request(config)
  }

  get(url, config) {
    return this.request({url, ...config, method: 'GET'})
  }

  post(url, config) {
    return this.request({url, ...config, method: 'POST'})
  }
}

const httpInstance = new $http(BASE_URL, TIMEOUT)
export default httpInstance